home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / PowerMacOberon feb96 / Source / TextFrames1.Mod (.txt) < prev    next >
Encoding:
Oberon Text  |  1996-01-29  |  63.0 KB  |  1,400 lines  |  [TEXT/.Ob4]

  1. Syntax10.Scn.Fnt
  2. Syntax10i.Scn.Fnt
  3. Syntax10b.Scn.Fnt
  4. Syntax8.Scn.Fnt
  5. Syntax12.Scn.Fnt
  6. Arial12.Scn.Fnt
  7. FoldElems
  8. MODULE TextFrames;    (** CAS/MH/HM 12.10.1993 / mf 12.10.93 / mah 26.7.94 **)
  9.     IMPORT Macintosh, Modules, Input, Display, Fonts, Viewers, Oberon, MenuViewers, Texts, ScrollBars, Files;
  10.     CONST
  11.         (** update message IDs **)
  12.             replace* = 0; insert* = 1; delete* = 2;
  13.         (** units **)
  14.             mm* = 36000;  Unit* = 10000;
  15.         (** parc options **)
  16.             gridAdj* = 0;  leftAdj* = 1;  rightAdj* = 2;  pageBreak* = 3;    twoColumns* = 4;
  17.         (** maximum number of TAB stops in Parc **)
  18.             MaxTabs* = 32;
  19.         AdjMask = {leftAdj, rightAdj};
  20.         TAB = 9X; CR = 0DX; DEL = 7FX; CRSL = 0C4X; CRSR = 0C3X; LF = 0A4X; BRK = 0B9X; ShiftBRK = 0B8X;
  21.         CRSU = 0C1X; CRSD = 0C2X; DELRIGHT = 008X;        (*<< mah *) 
  22.         EOL = 093X; HOME = 091X; PGUP = 0ACX; PGDN = 0ADX; (*<< mah *) 
  23.         AdjustSpan = 30; MinTabWidth = 3 * mm; StdTabWidth = 4 * mm;
  24.         rightKey = 0; middleKey = 1; leftKey = 2; cancel = {rightKey, middleKey, leftKey};
  25.         trackWord = 3; trackLine = 4;
  26.     TYPE
  27.         Parc* = POINTER TO ParcDesc;
  28.         ParcDesc* = RECORD (Texts.ElemDesc)
  29.             left*: LONGINT;    (** distance from (F.X + F.left); in units **)
  30.             first*: LONGINT;    (** first line indentation from P.left; in units **)
  31.             width*: LONGINT;    (** parc width; in units **)
  32.             lead*: LONGINT;    (** distance to previous line; in units **)
  33.             lsp*: LONGINT;    (** line spacing of text after P; in units **)
  34.             dsr*: LONGINT;    (** descender of text after P; in units **)
  35.             opts*: SET;
  36.             nofTabs*: INTEGER;
  37.             tab*: ARRAY MaxTabs OF LONGINT    (** in units **)
  38.         END;
  39.         TextLine = POINTER TO TextLineDesc;
  40.         Location* = RECORD
  41.             org*, pos*: LONGINT;
  42.             x*, y*, dx*, dy*: INTEGER;
  43.             line: TextLine
  44.         END;
  45.         TextLineDesc = RECORD
  46.             next: TextLine;
  47.             eot: BOOLEAN;                (* contains end of text *)    
  48.             indent: LONGINT;                (* line indentation in units *)
  49.             w, h, dsr: INTEGER;            (* bounding box clipped to frame (w including indent) *)
  50.             w0, nob: INTEGER;            (* unclipped width (including indent), number of contained blanks: nob > 0 if text line wraps around *)
  51.             org, len, span: LONGINT;    (* len ... characters w/o; span ... w/ trailing CR or white space, if any *)
  52.             P: Parc;                            (* last parc before this text line *)
  53.             pbeg: LONGINT                (* position of P *)
  54.         END;
  55.         Frame* = POINTER TO FrameDesc;
  56.         FrameDesc* = RECORD (Display.FrameDesc)
  57.             text*: Texts.Text;
  58.             org*: LONGINT;
  59.             col*, left*, right*, top*, bot*: INTEGER;
  60.             markH*: INTEGER;    (** position of tick mark in scroll bar (< 0 => no tick mark) **)
  61.             barW*: INTEGER;    (** scroll bar width **)
  62.             time*: LONGINT;    (** selection time **)
  63.             hasCar*, hasSel*, showsParcs*: BOOLEAN;    (** caret/selection present; parcs visible **)
  64.             carloc*, selbeg*, selend*: Location;
  65.             focus*: Display.Frame;    (** frame of nested element if this element contains the focus **)
  66.             trailer: TextLine    (* ring with trailer and header *)
  67.         END;
  68.         DisplayMsg* = RECORD (Texts.ElemMsg)
  69.             prepare*: BOOLEAN;
  70.             fnt*: Fonts.Font;
  71.             col*: SHORTINT;
  72.             pos*: LONGINT;    (** position in host text **)
  73.             frame*: Display.Frame;    (** ~prepare => host frame **)
  74.             X0*, Y0*: INTEGER;    (** ~prepare => receiver origin in screen space **)
  75.             indent*: LONGINT;    (** prepare => width already consumed in line, in units **)
  76.             elemFrame*: Display.Frame    (** optional return parameter **)
  77.         END;
  78.         TrackMsg* = RECORD (Texts.ElemMsg)
  79.             X*, Y*: INTEGER;
  80.             keys*: SET;
  81.             fnt*: Fonts.Font;
  82.             col*: SHORTINT;
  83.             pos*: LONGINT;    (** position in host text **)
  84.             frame*: Display.Frame;    (** host frame **)
  85.             X0*, Y0*: INTEGER    (** receiver origin in screen space **)
  86.         END;
  87.         FocusMsg* = RECORD (Texts.ElemMsg)
  88.             focus*: BOOLEAN;    (** whether to focus or to defocus **)
  89.             elemFrame*: Display.Frame;    (** focus/defocus target **)
  90.             frame*: Display.Frame    (** host frame **)
  91.         END;
  92.         NotifyMsg* = RECORD (Display.FrameMsg)
  93.             frame*: Display.Frame    (** host frame **)
  94.         END;
  95.         UpdateMsg* = RECORD (Display.FrameMsg)
  96.             id*: INTEGER;
  97.             text*: Texts.Text;
  98.             beg*, end*: LONGINT
  99.         END;
  100.         InsertElemMsg* = RECORD (Display.FrameMsg)
  101.             e*: Texts.Elem
  102.         END;
  103.         SelectMsg = RECORD (Display.FrameMsg)
  104.             text: Texts.Text;
  105.             beg, end: LONGINT;
  106.             time: LONGINT
  107.         END;
  108.         ScrollBarElem = POINTER TO ScrollBarElemDesc;                    (* CM 19.07.94 *)
  109.         ScrollBarElemDesc = RECORD (ScrollBars.ScrollBarElemDesc)
  110.         END;
  111.         menuH*, barW*, left*, right*, top*, bot*: INTEGER;
  112.         defParc*: Parc;
  113.         (*shared globals => get rid off in a later version?*)
  114.         W, W0: Texts.Writer;
  115.         B: Texts.Buffer;
  116.         P: Parc;
  117.         pbeg: LONGINT;    (*inv T[pbeg] = P*)
  118.         R: Texts.Reader;
  119.         nextCh: CHAR;    (*inv Base(R) = T => T[Pos(R)-1] = nextCh]*)
  120.         par: Oberon.ParList;
  121.         neutralize: Oberon.ControlMsg;
  122.         scrollBar: ScrollBarElem; used: BOOLEAN;                (*CM 08.08.94*)
  123.     PROCEDURE Min (x, y: INTEGER): INTEGER;
  124.     BEGIN IF x < y THEN RETURN x ELSE RETURN y END
  125.     END Min;
  126.     PROCEDURE Max (x, y: INTEGER): INTEGER;
  127.     BEGIN IF x > y THEN RETURN x ELSE RETURN y END
  128.     END Max;
  129.     PROCEDURE MarkMenu (F: Frame);
  130.         VAR R: Texts.Reader; V: Viewers.Viewer; T: Texts.Text; ch: CHAR;
  131.     BEGIN V := Viewers.This(F.X, F.Y);
  132.         IF (V IS MenuViewers.Viewer) & (V.dsc IS Frame) & (F # V.dsc) THEN
  133.             T := V.dsc(Frame).text;
  134.             IF T.len > 0 THEN Texts.OpenReader(R, T, T.len - 1); Texts.Read(R, ch) ELSE ch := 0X END;
  135.             IF ch # "!" THEN Texts.Write(W0, "!"); Texts.Append(T, W0.buf) END
  136.         END
  137.     END MarkMenu;
  138.     (* Element Subframes *)
  139.     PROCEDURE InvertBorder (F: Display.Frame);
  140.     BEGIN
  141.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y-1, F.W+2, 1, Display.invert);
  142.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y+F.H, F.W+2, 1, Display.invert);
  143.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y, 1, F.H, Display.invert);
  144.         Display.ReplPattern(Display.white, Display.grey1, F.X+F.W, F.Y, 1, F.H, Display.invert)
  145.     END InvertBorder;
  146.     PROCEDURE InvalSubFrames (F: Frame; x, y, w, h: INTEGER);    (* removes and suspends all subframes partly in (x, y, w, h) *)
  147.         VAR p, f: Display.Frame; msg: MenuViewers.ModifyMsg;
  148.     BEGIN
  149.         IF (w > 0) & (h > 0) THEN f := F.dsc;
  150.             IF f # NIL THEN p := f; f := p.next END;
  151.             WHILE f # NIL DO
  152.                 IF (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN p.next := f.next;
  153.                     msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  154.                     f.handle(f, msg)
  155.                 ELSE p := f
  156.                 END;
  157.                 f := p.next
  158.             END;
  159.             f := F.dsc;
  160.             IF (f # NIL) & (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN F.dsc := F.dsc.next;
  161.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  162.                 f.handle(f, msg)
  163.             END
  164.         END
  165.     END InvalSubFrames;
  166.     PROCEDURE ShiftSubFrames (F: Frame; oldY, newY, h: INTEGER);    (* shift (F.X, oldY, F.W, h) to (F.X, newY, F.W, h) *)
  167.         VAR f: Display.Frame; msg: MenuViewers.ModifyMsg;
  168.     BEGIN
  169.         IF oldY > newY THEN InvalSubFrames(F, F.X, newY, F.W, oldY - newY)
  170.         ELSE InvalSubFrames(F, F.X, oldY + h, F.W, newY - oldY)
  171.         END;
  172.         f := F.dsc;
  173.         WHILE f # NIL DO
  174.             IF (f.Y < oldY + h) & (f.Y + f.H > oldY) THEN INC(f.Y, newY - oldY);
  175.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := f.H;
  176.                 f.handle(f, msg)
  177.             END;
  178.             f := f.next
  179.         END
  180.     END ShiftSubFrames;
  181.     PROCEDURE NotifySubFrames (F: Frame; VAR msg: Display.FrameMsg);
  182.         VAR p, f: Display.Frame;
  183.     BEGIN f := F.dsc;
  184.         IF msg IS NotifyMsg THEN msg(NotifyMsg).frame := F END;
  185.         WHILE f # NIL DO p := f; f := f.next; p.handle(p, msg) END
  186.     END NotifySubFrames;
  187.     (* Display Primitives *)
  188.     PROCEDURE DrawCursor (x, y: INTEGER);
  189.     BEGIN Oberon.DrawCursor(Oberon.Mouse, Oberon.Arrow, x, y)
  190.     END DrawCursor;
  191.     PROCEDURE TrackMouse (VAR x, y: INTEGER; VAR keys, keysum: SET);
  192.     BEGIN Input.Mouse(keys, x, y); keysum := keysum + keys; DrawCursor(x, y)
  193.     END TrackMouse;
  194.     PROCEDURE EraseRect (F: Frame; x, y, w, h: INTEGER);
  195.     BEGIN
  196.         Display.ReplConst(F.col, x, y, w, h, Display.replace); InvalSubFrames(F, x, y, w, h)
  197.     END EraseRect;
  198.     PROCEDURE Erase (F: Frame; x, y, w, h: INTEGER);    (*RemoveMarks optimization*)
  199.     BEGIN
  200.         IF h > 0 THEN Oberon.RemoveMarks(x, y, w, h); EraseRect(F, x, y, w, h) END
  201.     END Erase;
  202.     PROCEDURE Shift (F: Frame; oldY, newY, h: INTEGER);    (*RemoveMarks optimization*)
  203.     BEGIN
  204.         IF (oldY # newY) & (h > 0) THEN
  205.             Oberon.RemoveMarks(F.X + F.left, Min(oldY, newY), F.W - F.left, Max(oldY, newY) + h);
  206.             Display.CopyBlock(F.X + F.left, oldY, F.W - F.left, h, F.X + F.left, newY, Display.replace);
  207.             ShiftSubFrames(F, oldY, newY, h)
  208.         END
  209.     END Shift;
  210.     PROCEDURE InvertCaret (F: Frame);
  211.         VAR loc: Location; bot: INTEGER;
  212.     BEGIN loc := F.carloc; bot := loc.y + loc.line.dsr - 6;
  213.         Display.CopyPatternC(F, Display.white, Display.hook, loc.x, bot, Display.invert)
  214.     END InvertCaret;
  215.     PROCEDURE InvertRect (F: Frame; x, y, w, h: INTEGER);    (*clips to right and bottom frame margin*)
  216.     BEGIN
  217.         IF x + w > F.X + F.W - F.right THEN w := F.X + F.W - F.right - x END;
  218.         IF y >= F.Y + F.bot THEN Display.ReplConst(Display.white, x, y, w, h, Display.invert) END
  219.     END InvertRect;
  220.     PROCEDURE InvertSelection (F: Frame; beg, end: Location);
  221.         VAR t: TextLine; ex, rx, w, py: INTEGER;
  222.     BEGIN
  223.         rx := F.X + F.W - F.right; t := end.line;
  224.         IF t.eot OR (end.pos <= t.org + t.len) THEN ex := end.x ELSE ex := rx END;
  225.         IF beg.line = end.line THEN InvertRect(F, beg.x, beg.y, ex - beg.x, beg.line.h)
  226.         ELSE t := beg.line; py := beg.y; w := F.W - F.left - F.right;
  227.             InvertRect(F, beg.x, py, rx - beg.x, t.h); t := t.next; DEC(py, t.h);
  228.             WHILE t # end.line DO InvertRect(F, F.X + F.left, py, w, t.h); t := t.next; DEC(py, t.h) END;
  229.             InvertRect(F, F.X + F.left, py, ex - (F.X + F.left), t.h)
  230.         END
  231.     END InvertSelection;
  232.     PROCEDURE CoordToPos (F: Frame; mh: INTEGER): LONGINT;
  233.         VAR h: INTEGER;
  234.     BEGIN h := F.H - 1;
  235.         IF h > 0 THEN RETURN (h - mh) * F.text.len DIV h ELSE RETURN 0 END
  236.     END CoordToPos;
  237.     PROCEDURE ShowBar (F: Frame; botH, topH: INTEGER);
  238.     BEGIN
  239.         IF (F.left > F.barW) & (F.barW >= barW) THEN                (*CM 08.08.94*)
  240.             scrollBar.F := F;
  241.             scrollBar.SetDim(F.X, F.Y, F.barW -1, F.H);
  242.             IF (F.text = NIL) OR (F.trailer = NIL) THEN  scrollBar.CalculateSliderDim(0,0,0)
  243.             ELSE scrollBar.CalculateSliderDim(F.text.len, F.org, F.trailer.org + F.trailer.len)
  244.             END;            
  245.             scrollBar.Draw;
  246.             Display.ReplConst(Display.white, F.X + F.barW - 1, F.Y + botH, 1, topH - botH, Display.replace)
  247.         END
  248.     END ShowBar;
  249.     PROCEDURE ShowTick (F: Frame);    (* removes global marks as needed *)
  250.         VAR h, mh: INTEGER; len: LONGINT;
  251.     BEGIN
  252.         h := F.H - 2; len := F.text.len;
  253.         IF len > 0 THEN mh := SHORT(h - h * F.org DIV len) ELSE mh := h END;
  254.         Oberon.RemoveMarks(F.X, F.Y, F.barW, F.H);                (*CM 08.08.94*)
  255.         IF ~used & (F.left > F.barW) & (F.barW >= barW) THEN
  256.             scrollBar.F := F;            
  257.             scrollBar.SetDim(F.X, F.Y, F.barW -1, F.H);            
  258.             scrollBar.CalculateSliderDim(F.text.len, F.org, F.trailer.org + F.trailer.len);
  259.             scrollBar.DrawSlider(FALSE)                                                    
  260.         END
  261.     END ShowTick;
  262.     PROCEDURE Mark* (F: Frame; mark: INTEGER);
  263.     BEGIN
  264. (*        Erase(F, F.X, F.Y, F.barW - 1, F.H); F.markH := -1;
  265.         IF mark > 0 THEN ShowTick(F) END *)
  266.     END Mark;
  267.     (** Parcs **)
  268.     PROCEDURE ParcBefore* (T: Texts.Text; pos: LONGINT; VAR P: Parc; VAR beg: LONGINT);
  269.         VAR R: Texts.Reader;
  270.     BEGIN Texts.OpenReader(R, T, pos + 1);
  271.         REPEAT Texts.ReadPrevElem(R) UNTIL R.eot OR (R.elem IS Parc);
  272.         IF R.eot THEN P := defParc; beg := -1 ELSE P := R.elem(Parc); beg := Texts.Pos(R) END
  273.     END ParcBefore;
  274.     PROCEDURE InitDefParc;
  275.     BEGIN
  276.         IF Modules.ThisMod("ParcElems") = NIL THEN HALT(99) END
  277.         (* side effect: body of ParcElems initialises defParc *)
  278.     END InitDefParc;
  279.     (* Screen Metrics *)
  280.     PROCEDURE Tab (dw: INTEGER; VAR dx: INTEGER);    (*P set*)
  281.         (* dw = line width from left margin to caret (in pixels); dx = distance from caret to next tab stop (in pixels) *)
  282.         VAR i, n: INTEGER; w: LONGINT;
  283.     BEGIN
  284.         i := 0; n := P.nofTabs; w := LONG(dw) * Unit + MinTabWidth;
  285.         IF dw < 0 THEN dx := -dw
  286.         ELSE
  287.             WHILE (i < n) & (P.tab[i] < w) DO INC(i) END;
  288.             IF i < n THEN dx := SHORT((P.tab[i] - LONG(dw) * Unit) DIV Unit)
  289.             ELSE dx := StdTabWidth DIV Unit
  290.             END
  291.         END
  292.     END Tab;
  293.     PROCEDURE MeasureSpecial (dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  294.         (* returns metrics of nextCh (nextCh <= " "); sends prepare message to elements; P, R, nextCh set *)
  295.         VAR e: Texts.Elem; pat: Display.Pattern; msg: DisplayMsg;
  296.     BEGIN
  297.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  298.             x := 0; y := 0; w := dx; h := 0
  299.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  300.         ELSIF R.elem # NIL THEN e := R.elem;
  301.             msg.prepare := TRUE; msg.indent := LONG(dw) * Unit;
  302.             msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R)-1;
  303.             msg.Y0 := -SHORT(P.dsr DIV Unit);    (*<<< 18-Nov-91*)
  304.             e.handle(e, msg);
  305.             w := SHORT(e.W DIV Unit);
  306.             dx := w; x := 0; y := msg.Y0; h := SHORT(e.H DIV Unit)    (*<<< 18-Nov-91*)
  307.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  308.         END
  309.     END MeasureSpecial;
  310.     PROCEDURE GetSpecial (F: Frame; VAR n: INTEGER; cn, ddx, dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  311.         (* returns metrics of nextCh (nextCh <= " "); no prepare message to elements; extends blanks for block adjust *)
  312.         (* cn ... add 1 pixel to first cn blanks (block adjust); ddx ... add ddx pixels to every blank (block adjust) *)
  313.         (*P, R, nextCh set*)
  314.         VAR e: Texts.Elem; pat: Display.Pattern;
  315.     BEGIN
  316.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  317.             x := 0; y := 0; INC(dx, ddx); INC(n); IF n <= cn THEN INC(dx) END;    (*space correction for block adjustment*)
  318.             w := dx; h := 0
  319.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  320.         ELSIF R.elem # NIL THEN e := R.elem;
  321.             IF (e IS Parc) & (P.W = 9999 * Unit) THEN (* P gets this value in prepare message *)
  322.                 w := Min(SHORT((P.width + P.left) DIV Unit), F.W - F.right - F.left);
  323.                 e.W := LONG(w) * Unit
  324.             ELSE w := SHORT(e.W DIV Unit)
  325.             END;
  326.             dx := w; x := 0; y := -SHORT(P.dsr DIV Unit); h := SHORT(e.H DIV Unit)
  327.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  328.         END
  329.     END GetSpecial;
  330.     PROCEDURE NextLine (T: Texts.Text; VAR org: LONGINT);    (*R, nextCh set; org = Texts.Pos(R)-1*)
  331.         VAR pat: Display.Pattern; pos, bk, d: LONGINT; width, tw, dx, x, y, w, h: INTEGER;
  332.             R1: Texts.Reader; peekCh: CHAR; indent: INTEGER;
  333.     BEGIN
  334.         tw := 0; dx := 0; w := 0; bk := -999;    (* bk = pos of last seperator *)
  335.         pos := org; ParcBefore(T, pos, P, pbeg); width := SHORT(P.width DIV Unit);
  336.         indent := 0;
  337.         IF org > 0 THEN Texts.OpenReader(R1, T, org - 1); Texts.Read(R1, peekCh);
  338.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN indent := SHORT(P.first DIV Unit) END;
  339.         END;
  340.         INC(tw, indent);
  341.         LOOP INC(pos);    (*inv pos = Texts.Pos(R), ~R.eof => nextCh = text[pos-1]*)
  342.             IF R.eot OR (nextCh = CR) THEN EXIT END;
  343.             INC(tw, dx);
  344.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  345.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  346.             END;
  347.             IF tw + x + w > width THEN d := pos - bk;
  348.                 IF (d < AdjustSpan) & (nextCh > " ") THEN pos := bk
  349.                 ELSIF ((nextCh > " ") OR (nextCh = Texts.ElemChar)) & (pos > org + 1) THEN DEC(pos)
  350.                 END;
  351.                 Texts.OpenReader(R, T, pos); Texts.Read(R, nextCh);
  352.                 EXIT
  353.             END;
  354.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN bk := pos END;
  355.             Texts.Read(R, nextCh)
  356.         END;
  357.         org := pos
  358.     END NextLine;
  359.     PROCEDURE BegOfLine (T: Texts.Text; VAR pos: LONGINT; adjust: BOOLEAN);
  360.         (* returns origin of line containing pos *)
  361.         VAR p, org: LONGINT;
  362.     BEGIN
  363.         IF pos <= 0 THEN pos := 0
  364.         ELSE
  365.             IF pos <= T.len THEN org := pos ELSE org := T.len END;
  366.             LOOP    (*search backwards for CR*)
  367.                 IF org = 0 THEN EXIT END;
  368.                 Texts.OpenReader(R, T, org - 1); Texts.Read(R, nextCh);
  369.                 IF nextCh = CR THEN EXIT END;
  370.                 DEC(org)
  371.             END;
  372.             IF adjust THEN    (*search forward for actual line origin*)
  373.                 Texts.OpenReader(R, T, org); Texts.Read(R, nextCh); p := org;
  374.                 REPEAT org := p; NextLine(T, p) UNTIL (p > pos) OR R.eot
  375.             END;
  376.             pos := org
  377.         END
  378.     END BegOfLine;
  379.     PROCEDURE AdjustMetrics (F: Frame; t: TextLine; VAR pw, tw, ddx, cn: INTEGER);    (*t.org set*)
  380.         (* pw ... x-coord of first char in line (in pixels); tw ... width of text line; ddx, cn ... see GetSpecial *)
  381.     BEGIN
  382.         P := t.P; pbeg := t.pbeg;
  383.         pw := F.left; tw := t.w; ddx := 0; cn := 0;
  384.         IF t.pbeg # t.org THEN
  385.             INC(pw, SHORT((P.left + t.indent) DIV Unit));
  386.             IF leftAdj IN P.opts THEN
  387.                 IF (rightAdj IN P.opts) & (t.nob > 0) THEN
  388.                     tw := SHORT(P.width DIV Unit); ddx := (tw - t.w0) DIV t.nob; cn := (tw - t.w0) MOD t.nob
  389.                 END
  390.             ELSIF rightAdj IN P.opts THEN INC(pw, SHORT(P.width DIV Unit) - t.w0)
  391.             ELSE (*center*) INC(pw, (SHORT(P.width DIV Unit) - t.w0) DIV 2)
  392.             END;
  393.             DEC(tw, SHORT(t.indent DIV Unit));
  394.         END
  395.     END AdjustMetrics;
  396.     (* Screen Placement *)
  397.     PROCEDURE DrawSpecial (F: Frame; px, py, x, y: INTEGER);    (*R, nextCh set*)
  398.         VAR e: Texts.Elem; pat: Display.Pattern; dx, w, h: INTEGER; msg: DisplayMsg;
  399.     BEGIN
  400.         IF (nextCh = TAB) OR (nextCh = CR) THEN (*skip*)
  401.         ELSIF R.elem # NIL THEN e := R.elem;
  402.             IF ~(e IS Parc) OR F.showsParcs THEN
  403.                 msg.prepare := FALSE; msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R) - 1;
  404.                 msg.frame := F; msg.X0 := px + x; msg.Y0 := py + y; msg.elemFrame := NIL;
  405.                 e.handle(e, msg);
  406.                 IF msg.elemFrame # NIL THEN msg.elemFrame.next := F.dsc; F.dsc := msg.elemFrame END;
  407.             ELSIF pageBreak IN e(Parc).opts THEN (*(e IS Parc) & ~F.showsParcs*)
  408.                 Display.ReplPattern(Display.white, Display.grey1, px + x, py, SHORT(e.W DIV Unit), 1, Display.replace)
  409.             END
  410.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  411.             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  412.         END;
  413.     END DrawSpecial;
  414.     PROCEDURE ShowLine (F: Frame; t: TextLine; left, right, py: INTEGER);
  415.         VAR pat: Display.Pattern; i: LONGINT; n, cn, lm, px, pw, tw, ddx, dx, x, y, w, h: INTEGER;
  416.     BEGIN
  417.         (* lm ... left parc margin in screen coord; pw ...  x of first char in frame coord *)
  418.         Texts.OpenReader(R, F.text, t.org); AdjustMetrics(F, t, pw, tw, ddx, cn);
  419.         lm := F.X + F.left + SHORT(P.left DIV Unit); px := F.X + pw; INC(py, t.dsr); i := 0; n := 0;
  420.         WHILE i < t.len DO Texts.Read(R, nextCh);
  421.             IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, px - lm, dx, x, y, w, h)
  422.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  423.             END;
  424.             INC(y, R.fnt.height * R.voff DIV 64);
  425.             IF px + x + w <= right THEN
  426.                 IF px + x >= left THEN
  427.                     IF nextCh <= " " THEN DrawSpecial(F, px, py, x, y)
  428.                     ELSE
  429.                         IF (R.col = Display.white) & ((F.col = Display.black) OR (F.col = Display.white)) THEN
  430.                             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  431.                         ELSE
  432.                             Display.CopyPattern(R.col, pat, px + x, py + y, Display.paint)
  433.                         END
  434.                     END
  435.                 END;
  436.                 INC(px, dx); INC(i)
  437.             ELSE i := t.len
  438.             END
  439.         END
  440.     END ShowLine;
  441.     PROCEDURE ShowLines (F: Frame; botH, topH: INTEGER; erase: BOOLEAN);
  442.         VAR t: TextLine; ph: INTEGER;
  443.     BEGIN
  444.         t := F.trailer.next; ph := F.H - F.top;
  445.         WHILE (t # F.trailer) & (ph - t.h >= topH) DO DEC(ph, t.h); t := t.next END;
  446.         WHILE (t # F.trailer) & (ph - t.h >= botH) DO DEC(ph, t.h);
  447.             IF erase THEN Erase(F, F.X + F.left, F.Y + ph, F.W - F.right - F.left, t.h) END;
  448.             ShowLine(F, t, F.X + F.left, F.X + F.W - F.right, F.Y + ph); t := t.next
  449.         END
  450.     END ShowLines;
  451.     (* Screen Casting *)
  452.     PROCEDURE MeasureLine (F: Frame; maxW: INTEGER; t: TextLine);    (* R, nextCh set *)
  453.         VAR pat: Display.Pattern; len, bklen, d: LONGINT; eol: BOOLEAN;
  454.             nob, bknob, width, minY, bkminY, maxY, bkmaxY, tw, bktw, lsp, dsr, dx, x, y, w, h: INTEGER;
  455.             R1: Texts.Reader; peekCh: CHAR;
  456.             (* bk* ... backup for last blank *)
  457.     BEGIN
  458.         len := 0; nob := 0; bklen := -999; tw := 0; dx := 0; minY := 0; maxY := 0;
  459.         ParcBefore(F.text, t.org, P, pbeg);
  460.         lsp := SHORT(P.lsp DIV Unit); dsr := SHORT(P.dsr DIV Unit); width := SHORT(P.width DIV Unit);
  461.         t.indent := 0;
  462.         IF t.org > 0 THEN Texts.OpenReader(R1, F.text, t.org - 1); Texts.Read(R1, peekCh);
  463.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN t.indent := P.first END;
  464.         END;
  465.         INC(tw, SHORT(t.indent DIV Unit));
  466.         LOOP
  467.             IF R.eot OR (nextCh = CR) THEN nob := 0; eol := ~R.eot; EXIT END;
  468.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  469.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  470.             END;
  471.             IF tw + x + w > width THEN d := len - bklen;
  472.                 IF (d < AdjustSpan) & (nextCh > " ") THEN eol := TRUE;
  473.                     Texts.OpenReader(R, F.text, Texts.Pos(R) - d);
  474.                     nob := bknob; len := bklen; tw := bktw; minY := bkminY; maxY := bkmaxY
  475.                 ELSIF len = 0 THEN    (* force at least one character on each line *)
  476.                     INC(len); INC(y, R.fnt.height * R.voff DIV 64); minY := Min(minY, y); maxY := Max(maxY, y + h);
  477.                     Texts.Read(R, nextCh); eol := FALSE; tw := maxW
  478.                 ELSE eol := (nextCh <= " ") & (nextCh # Texts.ElemChar)
  479.                 END;
  480.                 EXIT
  481.             END;
  482.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN
  483.                 bknob := nob; bklen := len; bktw := tw; bkminY := minY; bkmaxY := maxY;
  484.                 IF nextCh = " " THEN INC(nob) END
  485.             END;
  486.             INC(len); INC(tw, dx); INC(y, R.fnt.height * R.voff DIV 64);
  487.             IF y < minY THEN minY := y END;
  488.             IF y + h > maxY THEN maxY := y + h END;
  489.             Texts.Read(R, nextCh)
  490.         END;
  491.         IF ~F.showsParcs & (pbeg = t.org) THEN dsr := 0; t.h := SHORT(P.lead DIV Unit) + 1
  492.         ELSIF gridAdj IN P.opts THEN
  493.             WHILE dsr < -minY DO INC(dsr, lsp) END;
  494.             t.h := Max(lsp, dsr + maxY); INC(t.h, (-t.h) MOD lsp)
  495.         ELSE dsr := Max(dsr, -minY); t.h := Max(lsp, dsr + maxY)
  496.         END;
  497.         t.len := len; t.w0 := tw; t.w := Min(tw, maxW); t.dsr := dsr; t.nob := nob; t.eot := R.eot; t.P := P; t.pbeg := pbeg;
  498.         IF eol THEN Texts.Read(R, nextCh); t.span := len + 1 ELSE t.span := len END
  499.     END MeasureLine;
  500.     PROCEDURE MeasureLines (F: Frame; org: LONGINT; VAR trailer: TextLine);
  501.         VAR s, t: TextLine; ph: INTEGER;
  502.     BEGIN
  503.         NEW(trailer); s := trailer;
  504.         Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); ph := F.H - F.top;
  505.         LOOP NEW(t); t.org := org; MeasureLine(F, F.W - F.left - F.right, t);
  506.             IF ph - t.h < F.bot THEN EXIT END;
  507.             s.next := t; s := t; INC(org, s.span); DEC(ph, s.h);
  508.             IF R.eot THEN EXIT END
  509.         END;
  510.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; (* start of first invisible line *) trailer.len := 0; trailer.w := 0;
  511.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P (* P set by MeasureLine *) ; trailer.pbeg := pbeg
  512.     END MeasureLines;
  513.     (** Locators **)
  514.     PROCEDURE LocateLineTop (F: Frame; trailer: TextLine; org: LONGINT; VAR loc: Location);
  515.         VAR t: TextLine; ph: INTEGER;
  516.     BEGIN
  517.         ph := F.H - F.top; t := trailer.next;
  518.         WHILE (t # trailer) & (t.org # org) DO DEC(ph, t.h); t := t.next END;
  519.         loc.org := org; loc.line := t; loc.y := F.Y + ph
  520.     END LocateLineTop;
  521.     PROCEDURE Width (F: Frame; t: TextLine; pos: LONGINT; VAR pw, dx, dy: INTEGER);
  522.         VAR pat: Display.Pattern; i: LONGINT; n, mw, lm, tw, ddx, cn, x, y, w, h: INTEGER;
  523.     BEGIN
  524.         AdjustMetrics(F, t, pw, tw, ddx, cn); dy := 0; lm := F.left + SHORT(P.left DIV Unit);
  525.         IF t # F.trailer THEN Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh);
  526.             i := 0; n := 0; DEC(pos, t.org); dx := 0; mw := F.W - F.right;
  527.             WHILE ~R.eot & (i < t.len) & (i <= pos) & (pw + dx <= mw) DO
  528.                 (* i ... pos of nextCh; dx ... width of char before nextCh; pw ... line width up to pos (or up to right margin) *)
  529.                 INC(i); INC(pw, dx);
  530.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, x, y, w, h)
  531.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  532.                 END;
  533.                 dy := R.fnt.height * R.voff DIV 64;
  534.                 Texts.Read(R, nextCh)
  535.             END;
  536.             IF (i <= pos) & (pw + dx <= mw) THEN INC(i); INC(pw, dx) END
  537.         ELSE dx := 4
  538.         END
  539.     END Width;
  540.     PROCEDURE LocatePos* (F: Frame; pos: LONGINT; VAR loc: Location);    (* loc.dx = dx of char at pos *)
  541.         VAR t: TextLine; pw, dx, dy: INTEGER;
  542.     BEGIN
  543.         IF pos < F.org THEN pos := F.org; t := F.trailer.next
  544.         ELSIF pos < F.trailer.org THEN t := F.trailer;
  545.             WHILE (t.next # F.trailer) & (t.next.org <= pos) DO t := t.next END
  546.         ELSE pos := F.trailer.org; t := F.trailer.next;
  547.             WHILE ~t.eot DO t := t.next END
  548.         END;
  549.         Width(F, t, pos, pw, dx, dy); LocateLineTop(F, F.trailer, t.org, loc); DEC(loc.y, loc.line.h);
  550.         loc.org := t.org; loc.pos := pos; loc.x := F.X + pw; loc.dx := dx; loc.dy := dy; loc.line := t
  551.     END LocatePos;
  552.     PROCEDURE LocateLine* (F: Frame; y: INTEGER; VAR loc: Location);
  553.         (* loc.x = line start; loc.y = line bottom; loc.dx = line width *)
  554.         VAR t: TextLine; h, ph, pw, tw, ddx, cn: INTEGER;
  555.     BEGIN
  556.         t := F.trailer.next; h := y - F.Y; ph := F.H - F.top - t.h;
  557.         WHILE ~t.eot & (ph - t.next.h >= F.bot) & (ph > h) DO t := t.next; DEC(ph, t.h) END;
  558.         AdjustMetrics(F, t, pw, tw, ddx, cn);
  559.         IF pw >= F.W - F.right THEN pw := F.W - F.right - 4 END;
  560.         loc.org := t.org; loc.pos := loc.org; loc.x := F.X + pw; loc.y := F.Y + ph; loc.dx := tw; loc.dy := 0; loc.line := t
  561.     END LocateLine;
  562.     PROCEDURE LocateChar* (F: Frame; x, y: INTEGER; VAR loc: Location);
  563.         VAR t: TextLine; pat: Display.Pattern; i: LONGINT; n, w, lm, pw, tw, ddx, cn, dx, xc, yc, wc, hc: INTEGER;
  564.     BEGIN
  565.         LocateLine(F, y, loc); t := loc.line; w := x - F.X; AdjustMetrics(F, t, pw, tw, ddx, cn);
  566.         lm := F.left + SHORT(P.left DIV Unit);
  567.         IF (t # F.trailer) & (w > pw) THEN Texts.OpenReader(R, F.text, t.org);
  568.             i := 0; n := 0; dx := 0; nextCh := 0X;
  569.             WHILE (i < t.len) & (pw + dx < w) DO
  570.                 (* i = pos after nextCh; dx = width of nextCh; pw = line width without nextCh *)
  571.                 Texts.Read(R, nextCh); INC(i); INC(pw, dx);
  572.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, xc, yc, wc, hc)
  573.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  574.                 END
  575.             END;
  576.             IF pw + dx < w THEN INC(i); INC(pw, dx); R.elem := NIL END;
  577.             INC(loc.pos, i - 1); loc.x := F.X + pw;
  578.             IF i < t.len THEN loc.dx := dx; loc.dy := R.fnt.height * R.voff DIV 64 ELSE loc.dx := 4 END
  579.         ELSE loc.dx := 4; R.elem := NIL
  580.         END
  581.     END LocateChar;
  582.     PROCEDURE LocateWord* (F: Frame; x, y: INTEGER; VAR loc: Location);
  583.         VAR t: TextLine; pos, i: LONGINT; px, rx: INTEGER; pat: Display.Pattern; dx, xc, yc, wc, hc: INTEGER;
  584.     BEGIN
  585.         LocateChar(F, x, y, loc); pos := loc.pos + 1;
  586.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  587.         UNTIL (pos < loc.org) OR (nextCh > " ");
  588.         INC(pos);
  589.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  590.         UNTIL (pos < loc.org) OR (nextCh <= " ");
  591.         LocatePos(F, pos + 1, loc); t := loc.line; i := loc.pos - loc.org;
  592.         IF i < t.len THEN px := loc.x; rx := F.X + F.W - F.right;
  593.             Texts.OpenReader(R, F.text, loc.pos); dx := 0; wc := 0; nextCh := "x";
  594.             WHILE (i < t.len) & (nextCh > " ") & (px + dx < rx) DO
  595.                 Texts.Read(R, nextCh); INC(i); INC(px, dx);
  596.                 Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  597.             END;
  598.             IF (nextCh > " ") & (px + dx < rx) THEN INC(i); INC(px, dx) END;
  599.             loc.dx := px - loc.x
  600.         ELSE loc.dx := 0
  601.         END
  602.     END LocateWord;
  603.     PROCEDURE Pos* (F: Frame; x, y: INTEGER): LONGINT;
  604.         VAR loc: Location;
  605.     BEGIN LocateChar(F, x, y, loc); RETURN loc.pos
  606.     END Pos;
  607.     PROCEDURE WordBeg (t: Texts.Text; pos: LONGINT): LONGINT;
  608.         VAR r: Texts.Reader; ch: CHAR; pos0: LONGINT;
  609.     BEGIN
  610.         pos0 := pos; Texts.OpenReader(r, t, pos); Texts.Read(r, ch);
  611.         WHILE (CAP(ch) >= "A") & (CAP(ch) <= "Z") OR (ch >= "0") & (ch <= "9") DO
  612.             DEC(pos); IF pos < 0 THEN RETURN 0 END;
  613.             Texts.OpenReader(r, t, pos); Texts.Read(r, ch)
  614.         END;
  615.         IF pos < pos0 THEN INC(pos) END;
  616.         RETURN pos
  617.     END WordBeg;
  618.     PROCEDURE WordEnd (t: Texts.Text; pos: LONGINT): LONGINT;
  619.         VAR r: Texts.Reader; ch: CHAR; pos0: LONGINT;
  620.     BEGIN
  621.         pos0 := pos; Texts.OpenReader(r, t, pos); Texts.Read(r, ch);
  622.         WHILE (CAP(ch) >= "A") & (CAP(ch) <= "Z") OR (ch >= "0") & (ch <= "9") DO
  623.             INC(pos); Texts.Read(r, ch)
  624.         END;
  625.         IF pos = pos0 THEN INC(pos) END;
  626.         RETURN pos
  627.     END WordEnd;
  628.     PROCEDURE ThisSubFrame (F: Frame; x, y: INTEGER): Display.Frame;
  629.         VAR f: Display.Frame;
  630.     BEGIN f := F.dsc;
  631.         WHILE (f # NIL) & ((x < f.X) OR (x >= f.X + f.W) OR (y < f.Y) OR (y >= f.Y + f.H)) DO f := f.next END;
  632.         RETURN f
  633.     END ThisSubFrame;
  634.     (** Caret & Selection **)
  635.     PROCEDURE PassSubFocus (F: Frame; f: Display.Frame);
  636.         (* pass focus from F.focus to f (f is also an element frame in F) *)
  637.         VAR loc: Location; f1: Display.Frame; ctrl: Oberon.ControlMsg; focus: FocusMsg;
  638.     BEGIN
  639.         IF F.focus # NIL THEN f1 := F.focus;
  640.             ctrl.id := Oberon.defocus; f1.handle(f1, ctrl);
  641.             LocateChar(F, f1.X + 1, f1.Y + 1, loc);
  642.             InvertBorder(f1); F.focus := NIL;
  643.             IF R.elem # NIL THEN
  644.                 focus.focus := FALSE; focus.elemFrame := f1; focus.frame := F; R.elem.handle(R.elem, focus)
  645.             END
  646.         END;
  647.         IF f # NIL THEN
  648.             LocateChar(F, f.X + 1, f.Y + 1, loc);    (* side effect: set R to element *)
  649.             focus.focus := TRUE; focus.elemFrame := f; focus.frame := F; R.elem.handle(R.elem, focus);
  650.             InvertBorder(f)
  651.         END;
  652.         F.focus := f
  653.     END PassSubFocus;
  654.     PROCEDURE RemoveSelection* (F: Frame);
  655.     BEGIN
  656.         IF F.hasSel THEN InvertSelection(F, F.selbeg, F.selend); F.hasSel := FALSE END
  657.     END RemoveSelection;
  658.     PROCEDURE SetSelection* (F: Frame; beg, end: LONGINT);    (** forces range to visible bounds **)
  659.         VAR loc: Location;
  660.     BEGIN
  661.         IF end > F.text.len THEN end := F.text.len END;
  662.         IF end > beg THEN
  663.             IF F.hasSel & (F.selbeg.pos = beg) THEN
  664.                 IF (F.selend.pos < end) & (F.selend.pos < F.trailer.org) THEN
  665.                     LocatePos(F, F.selend.pos, loc); LocatePos(F, end, F.selend); InvertSelection(F, loc, F.selend)
  666.                 ELSIF end < F.selend.pos THEN
  667.                     LocatePos(F, end, loc); InvertSelection(F, loc, F.selend); LocatePos(F, end, F.selend)
  668.                 END
  669.             ELSE RemoveSelection(F); PassSubFocus(F, NIL);
  670.                 LocatePos(F, beg, F.selbeg); LocatePos(F, end, F.selend); InvertSelection(F, F.selbeg, F.selend)
  671.             END;
  672.             F.hasSel := TRUE; F.time := Oberon.Time()
  673.         END
  674.     END SetSelection;
  675.     PROCEDURE RemoveCaret* (F: Frame);
  676.         VAR msg: Oberon.ControlMsg;
  677.     BEGIN
  678.         IF F.focus # NIL THEN msg.id := Oberon.defocus; F.focus.handle(F.focus, msg) END;
  679.         IF F.hasCar THEN InvertCaret(F); F.hasCar := FALSE END
  680.     END RemoveCaret;
  681.     PROCEDURE SetCaret* (F: Frame; pos: LONGINT);    (** only done if within visible bounds **)
  682.     BEGIN
  683.         IF ~F.hasCar OR (F.carloc.pos # pos) THEN RemoveCaret(F); PassSubFocus(F, NIL);
  684.             LocatePos(F, pos, F.carloc);
  685.             IF F.carloc.x <= F.X + F.W - F.right THEN InvertCaret(F); F.hasCar := TRUE END
  686.         END
  687.     END SetCaret;
  688.     (** Display Range **)
  689.     PROCEDURE Complete (F: Frame; trailer: TextLine; s: TextLine; org: LONGINT; ph: INTEGER);
  690.         VAR u: TextLine;
  691.     BEGIN
  692.         IF ph > F.bot THEN    (*try to add new lines to the bottom*)
  693.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);
  694.             LOOP
  695.                 IF R.eot THEN EXIT END;
  696.                 NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  697.                 IF ph - u.h < F.bot THEN EXIT END;
  698.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span)
  699.             END
  700.         END;
  701.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; trailer.len := 0; trailer.w := 0;
  702.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P; trailer.pbeg := pbeg
  703.     END Complete;
  704.     PROCEDURE ShowFrom (F: Frame; pos: LONGINT);    (* removes global marks as needed and neutralizes F *)
  705.         VAR new, s: TextLine; beg, end: Location; org: LONGINT; ph, y0, dy: INTEGER;
  706.     BEGIN
  707.         F.handle(F, neutralize);
  708.         IF (F.trailer # NIL) & (F.org < pos) & (pos < F.trailer.org) THEN    (* shift up and extend to the bottom *)
  709.             LocateLineTop(F, F.trailer, pos, beg); LocateLineTop(F, F.trailer, F.trailer.org, end);
  710.             dy := (F.Y + F.H - F.top) - beg.y; Shift(F, end.y, end.y + dy, beg.y - end.y);
  711.             Erase(F, F.X + F.left, end.y, F.W - F.left, dy);
  712.             s := F.trailer.next; WHILE s.org # pos DO s := s.next END;
  713.             F.trailer.next := s; org := s.org + s.span; ph := F.H - F.top - s.h;
  714.             WHILE s.next # F.trailer DO s := s.next; org := org + s.span; ph := ph - s.h END;
  715.             Complete(F, F.trailer, s, org, ph); F.org := pos; ShowLines(F, F.bot, end.y + dy - F.Y, FALSE)
  716.         ELSIF (F.trailer = NIL) OR (pos # F.org) THEN
  717.             MeasureLines(F, pos, new);
  718.             IF (F.trailer # NIL) & (pos < F.org) & (F.org <= new.org) THEN    (* shift down and extend to the top *)
  719.                 LocateLineTop(F, new, F.org, beg); LocateLineTop(F, new, new.org, end);
  720.                 y0 := F.Y + F.H - F.top; Shift(F, y0 - (beg.y - end.y), end.y, beg.y - end.y);
  721.                 Erase(F, F.X + F.left, beg.y, F.W - F.left, y0 - beg.y);
  722.                 Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, end.y - (F.Y + F.bot));
  723.                 F.org := pos; F.trailer := new; ShowLines(F, beg.y - F.Y, F.H - F.top, FALSE)
  724.             ELSE    (* full redisplay *)
  725.                 IF F.trailer = NIL THEN Erase(F, F.X, F.Y, F.W, F.H); ShowBar(F, 0, F.H); F.markH := -1
  726.                 ELSE Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, F.H - F.bot - F.top)
  727.                 END;
  728.                 F.org := pos; F.trailer := new; ShowLines(F, F.bot, F.H - F.top, FALSE)
  729.             END
  730.         END;
  731.         ShowTick(F)
  732.     END ShowFrom;
  733.     PROCEDURE Show* (F: Frame; pos: LONGINT);    (** removes global marks as needed and neutralizes F **)
  734.     BEGIN BegOfLine(F.text, pos, TRUE); ShowFrom(F, pos)
  735.     END Show;
  736.     PROCEDURE Resize (F: Frame; x, y, w, h: INTEGER);
  737.         VAR oldY, oldH, dh, ph: INTEGER; t: TextLine;
  738.     BEGIN
  739.         IF (w = 0) OR (h = 0) THEN InvalSubFrames(F, F.X, F.Y, F.W, F.H);
  740.             F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL
  741.         ELSIF (F.trailer # NIL) & (x = F.X) & (w = F.W) THEN
  742.             oldY := F.Y; oldH := F.H; F.markH := -1; F.Y := y; F.H := h;
  743.             IF h > oldH THEN dh := h - oldH;    (* extend *)
  744.                 IF y + h # oldY + oldH THEN
  745.                     Display.CopyBlock(x+F.barW, oldY, w-F.barW, oldH, x+F.barW, y + dh, Display.replace);    (*CM 08.08.94*)
  746.                     ShiftSubFrames(F, oldY, y + dh, oldH)
  747.                 END;
  748.                 EraseRect(F, x, y, w, dh);                                                                                                (*CM 08.08.94*)
  749.                 t := F.trailer; ph := F.H - F.top;
  750.                 WHILE t.next # F.trailer DO t := t.next; ph := ph - t.h END;
  751.                 Complete(F, F.trailer, t, F.trailer.org, ph); ShowLines(F, F.bot, ph, FALSE)
  752.             ELSE dh := oldH - h;    (* reduce *)
  753.                 IF y + h # oldY + oldH THEN
  754.                     Display.CopyBlock(x+F.barW, oldY + dh, w-F.barW, h, x+F.barW, y, Display.replace);    (*CM 08.08.94*)
  755.                     ShiftSubFrames(F, oldY + dh, y, h)
  756.                 END;
  757.                 t := F.trailer; ph := F.H - F.top;
  758.                 WHILE (t.next # F.trailer) & (ph - t.next.h >= F.bot) DO t := t.next; DEC(ph, t.h) END;
  759.                 IF t = F.trailer THEN t.org := F.org; t.span := 0 END;
  760.                 Complete(F, F.trailer, t, t.org + t.span, ph);
  761.                 EraseRect(F, x + F.left, y, w - F.left, ph);
  762.                 InvalSubFrames(F, x, oldY, w, y - oldY); InvalSubFrames(F, x, y + h, w, dh - (y - oldY))
  763.             END;
  764.             ShowBar(F,0,h);                                                                                                        (*CM 08.08.94*)
  765.             ShowTick(F)
  766.         ELSE F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL; Show(F, F.org); ShowBar(F,0,h)
  767.         END
  768.     END Resize;
  769.     (** Contents Update **)
  770.     PROCEDURE Update (F: Frame; VAR msg: UpdateMsg);    (** removes global marks as needed **)
  771.         VAR t: TextLine; org, d: LONGINT;
  772.         PROCEDURE Begin (VAR beg: LONGINT; VAR org0: LONGINT; VAR q: TextLine);
  773.             (* org0 = origin of first affected line; beg = pos of first modified character; q = first affected line (if line origin has not moved).*)
  774.             (* q = NIL => beg = org0; q # NIL => first (beg-org0) characters of q need not be redrawn *)
  775.             VAR trailer, t: TextLine;
  776.         BEGIN
  777.             trailer := F.trailer; t := trailer; q := NIL;
  778.             WHILE (t.next # trailer) & (beg >= t.next.org + t.next.span) & ~t.next.eot DO t := t.next END;
  779.             q := t.next; org0 := beg; BegOfLine(F.text, org0, TRUE);
  780.             IF (org0 # q.org) OR (q = trailer) THEN
  781.                 IF org0 > q.org THEN org0 := q.org END;
  782.                 beg := org0; q := NIL
  783.             END
  784.         END Begin;
  785.         PROCEDURE Adjust (end, delta: LONGINT);
  786.             (* H1 = top of synchronization line in old frame *)
  787.             (* h0 = top of line that was modified *)
  788.             (* h1 = top of block in new frame that could be reused *)
  789.             (* h2 = bottom of last line in new frame *)
  790.             (* h1 - h2 = height of block that could be reused *)
  791.             VAR new, old, s, t, u, p, q: TextLine; bot: Location;
  792.                 org, org0, beg: LONGINT; ph, h0, h1, H1, h2, lm, dx, dy: INTEGER;
  793.         BEGIN
  794.             q := NIL; LocateLineTop(F, F.trailer, F.trailer.org, bot);
  795.             IF msg.beg < F.org THEN org0 := F.org; beg := org0 ELSE beg := msg.beg; Begin(beg, org0, q) END;
  796.             NEW(new); s := new; old := F.trailer; t := old; org := F.org; ph := F.H - F.top;
  797.             WHILE (t.next # old) & (t.next.org # org0) DO t := t.next;    (*transfer unchanged prefix*)
  798.                 s.next := t; s := t; DEC(ph, s.h); INC(org, s.span)
  799.             END;
  800.             h0 := ph; H1 := h0; t := t.next; p := s;
  801.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);    (*rebuild at least one line descriptor*)
  802.             LOOP NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  803.                 IF ph - u.h < F.bot THEN h1 := ph; h2 := h1; EXIT END;
  804.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span);
  805.                 IF R.eot THEN h1 := ph; h2 := h1; EXIT END;
  806.                 IF org > end THEN
  807.                     WHILE (t # old) & (org > t.org + delta) DO DEC(H1, t.h); t := t.next END;
  808.                     IF (org = t.org + delta) & (P = t.P) THEN h1 := ph;    (*resynchronized*)
  809.                         WHILE (t # old) & (ph - t.h >= F.bot) DO    (*transfer unchanged suffix*)
  810.                             s.next := t; s := t; s.org := org; ParcBefore(F.text, s.org, s.P, s.pbeg);
  811.                             DEC(ph, s.h); INC(org, s.span); t := t.next
  812.                         END;
  813.                         h2 := ph; EXIT
  814.                     END
  815.                 END
  816.             END;
  817.             Shift(F, F.Y + H1 - (h1 - h2), F.Y + h2, h1 - h2);
  818.             Complete(F, new, s, org, ph); F.trailer := new; t := p.next;
  819.             IF (q # NIL) & (q.h = t.h) & (q.dsr = t.dsr) & (q.org = t.org) & (q.P = t.P) & (end <= t.org + t.span) THEN
  820.                 P := t.P; pbeg := t.pbeg;
  821.                 IF (P.opts * AdjMask = {leftAdj}) OR (P.opts * AdjMask = AdjMask) & (q.nob = 0) & (t.nob = 0) THEN
  822.                     Width(F, t, beg, lm, dx, dy);    (*preserve prefix of first affected line*)
  823.                     DEC(h0, t.h); Erase(F, F.X + lm, F.Y + h0, F.W - lm, t.h);
  824.                     ShowLine(F, t, F.X + lm, F.X + F.W - F.right, F.Y + h0)
  825.                 END
  826.             END;
  827.             ShowLines(F, h1, h0, TRUE);
  828.             Erase(F, F.X + F.left, bot.y, F.W - F.left, h2 - (bot.y - F.Y)); ShowLines(F, F.bot, h2, FALSE)
  829.         END Adjust;
  830.     BEGIN F.handle(F, neutralize); MarkMenu(F);
  831.         IF (msg.id = Texts.insert) & (msg.beg < F.org) THEN t := F.trailer; d := msg.end - msg.beg; INC(F.org, d);
  832.             REPEAT INC(t.org, d); t := t.next UNTIL t = F.trailer
  833.         ELSIF (msg.id = Texts.delete) & (msg.end <= F.org) THEN t := F.trailer; d := msg.end - msg.beg; DEC(F.org, d);
  834.             REPEAT DEC(t.org, d); t := t.next UNTIL t = F.trailer
  835.         END;
  836.         org := F.org;
  837.         IF msg.beg <= F.org + AdjustSpan THEN BegOfLine(F.text, org, TRUE) END;
  838.         ParcBefore(F.text, org, P, d);
  839.         IF (org # F.org) OR (P # F.trailer.next.P) THEN
  840.             F.trailer := NIL; Show(F, F.org)
  841.         ELSIF (msg.end > org) & (msg.beg < F.trailer.org + AdjustSpan) THEN
  842.             IF msg.id = Texts.replace THEN Adjust(msg.end, 0)
  843.             ELSIF msg.id = Texts.insert THEN Adjust(msg.end, msg.end - msg.beg)
  844.             ELSIF msg.id = Texts.delete THEN Adjust(msg.beg, msg.beg - msg.end)
  845.             END
  846.         END;
  847.         ShowTick(F)
  848.     END Update;
  849.     (** User Interface **)
  850.     PROCEDURE Back (F: Frame; dY: INTEGER; (*inout*) VAR org: LONGINT);    (* mh 10.10.92 *)
  851.         (* computes new org such that old org is (at most) dY pixels below new org *)
  852.         VAR H: INTEGER; oldOrg: LONGINT;
  853.         PROCEDURE TotalHeight (org1, org2: LONGINT): INTEGER;
  854.             (* measures total height of text-lines starting at org1 and ending at the line before the line containing org2 *) 
  855.             VAR h: INTEGER; line: TextLine;
  856.         BEGIN
  857.             Texts.OpenReader(R, F.text, org1); Texts.Read(R, nextCh); NEW(line); h := 0;
  858.             LOOP line.org := org1;
  859.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org1, line.span);
  860.                 IF Texts.Pos(R)-1 > org2 THEN EXIT END;
  861.                 INC(h, line.h);
  862.                 IF R.eot THEN EXIT END;
  863.             END;
  864.             RETURN h
  865.         END TotalHeight;
  866.         PROCEDURE Forward (h: INTEGER);
  867.             (* increase org by n text-lines such that the sum of the n line-heights > h *)
  868.             VAR line: TextLine;
  869.         BEGIN
  870.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); NEW(line);
  871.             WHILE h > 0 DO line.org := org;
  872.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org, line.span); DEC(h, line.h);
  873.             END;
  874.             org := Texts.Pos(R)-1;
  875.         END Forward;
  876.     BEGIN H := 0;
  877.         LOOP oldOrg := org;
  878.             IF org = 0 THEN EXIT END;
  879.             DEC(org, 800); BegOfLine(F.text, org, FALSE);
  880.             INC(H, TotalHeight(org, oldOrg));
  881.             IF H > dY THEN EXIT END;
  882.         END;
  883.         Forward(H - dY);
  884.     END Back;
  885.     PROCEDURE TrackLine* (F: Frame; VAR x, y: INTEGER; VAR org: LONGINT; VAR keysum: SET);
  886.         VAR keys: SET; new, old: Location;
  887.     BEGIN
  888.         LocateLine(F, y, old); InvertRect(F, old.x, old.y, old.dx + 4, 2); keysum := {};
  889.         REPEAT TrackMouse(x, y, keys, keysum); LocateLine(F, y, new);
  890.             IF new.org # old.org THEN
  891.                 InvertRect(F, new.x, new.y, new.dx + 4, 2); InvertRect(F, old.x, old.y, old.dx + 4, 2); old := new
  892.             END
  893.         UNTIL keys = {};
  894.         InvertRect(F, new.x, new.y, new.dx + 4, 2); org := new.org
  895.     END TrackLine;
  896.     PROCEDURE TrackWord* (F: Frame; VAR x, y: INTEGER; VAR pos: LONGINT; VAR keysum: SET);
  897.         VAR keys: SET; new, old: Location;
  898.     BEGIN 
  899.         LocateWord(F, x, y, old); InvertRect(F, old.x, old.y, old.dx, 2); keysum := {};
  900.         REPEAT TrackMouse(x, y, keys, keysum); LocateWord(F, x, y, new);
  901.             IF new.pos # old.pos THEN
  902.                 InvertRect(F, new.x, new.y, new.dx, 2); InvertRect(F, old.x, old.y, old.dx, 2); old := new
  903.             END
  904.         UNTIL keys = {};
  905.         InvertRect(F, new.x, new.y, new.dx, 2); pos := new.pos
  906.     END TrackWord;
  907.     PROCEDURE TrackCaret* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  908.         VAR keys: SET;
  909.     BEGIN keysum := {};
  910.         REPEAT TrackMouse(x, y, keys, keysum); SetCaret(F, Pos(F, x, y)) UNTIL keys = {}
  911.     END TrackCaret;
  912.     PROCEDURE TrackSelection* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  913.         VAR keys: SET; pos, pos0, beg, end: LONGINT; V: Viewers.Viewer; f: Frame; loc: Location;
  914.     BEGIN
  915.         IF F.hasSel THEN beg := F.selbeg.pos; end := F.selend.pos
  916.         ELSE beg := Pos(F, x, y); end := beg + 1; SetSelection(F, beg, end)
  917.         END;
  918.         V := Viewers.This(F.X, F.Y); V := V.next(Viewers.Viewer);
  919.         IF (V.dsc # NIL) & (V.dsc.next # NIL) & (V.dsc.next IS Frame) THEN f := V.dsc.next(Frame);
  920.             IF f.hasSel & (f.text = F.text) THEN
  921.                 IF (f.selbeg.pos < f.trailer.org) & (f.org < f.selend.pos) & (f.selbeg.pos <= Pos(F, x, y)) THEN
  922.                     beg := f.selbeg.pos; SetSelection(F, beg, end)
  923.                 ELSE RemoveSelection(f); f := NIL
  924.                 END
  925.             ELSE f := NIL
  926.             END
  927.         ELSE f := NIL
  928.         END;
  929.         keysum := keysum - {0..2}; pos0 := beg;
  930.         REPEAT
  931.             TrackMouse(x, y, keys, keysum); pos := Pos(F, x, y); 
  932.             IF pos < pos0 THEN end := pos0;
  933.                 IF trackLine IN keysum THEN LocateLine(F, y, loc); beg := loc.org
  934.                 ELSIF trackWord IN keysum THEN beg := WordBeg(F.text, pos) 
  935.                 ELSE beg := pos 
  936.                 END
  937.             ELSE beg := pos0;
  938.                 IF trackLine IN keysum THEN LocateLine(F, y, loc); end := loc.org + loc.line.span
  939.                 ELSIF trackWord IN keysum THEN end := WordEnd(F.text, pos) 
  940.                 ELSE end := pos + 1 
  941.                 END
  942.             END;
  943.             IF beg >= F.org THEN SetSelection(F, beg, end) ELSE SetSelection(F, F.org, end) END;
  944.             IF f # NIL THEN SetSelection(f, beg, end) END
  945.         UNTIL keys = {};
  946.         IF f # NIL THEN F.selbeg.pos := beg END
  947.     END TrackSelection;
  948.     PROCEDURE Call (F: Frame; pos: LONGINT; new: BOOLEAN);
  949.         VAR S: Texts.Scanner; res, i, j: INTEGER;
  950.     BEGIN
  951.         Texts.OpenScanner(S, F.text, pos); Texts.Scan(S);
  952.         IF (S.class = Texts.Name) & (S.line = 0) THEN
  953.             i := 0; WHILE (i < S.len) & (S.s[i] # ".") DO INC(i) END;
  954.             j := i + 1; WHILE (j < S.len) & (S.s[j] # ".") DO INC(j) END;
  955.             IF (j >= S.len) & (S.s[i] = ".") THEN
  956.                 par.vwr := Viewers.This(F.X, F.Y);
  957.                 par.frame := F; par.text := F.text; par.pos := pos + S.len;
  958.                 Oberon.Call(S.s, par, new, res);
  959.                 IF res > 0 THEN
  960.                     Texts.WriteString(W0, "Call error: "); Texts.WriteString(W0, Modules.importing);    (* mf *)
  961.                     IF res=1 THEN Texts.WriteString(W0, " not found")
  962.                     ELSIF res=2 THEN Texts.WriteString(W0, " not a valid object file")
  963.                     ELSIF res=3 THEN Texts.WriteString(W0, " imports "); Texts.WriteString(W0, Modules.imported);
  964.                         Texts.WriteString(W0, " with bad key")
  965.                     ELSIF res=4 THEN Texts.WriteString(W0, " not enough memory")
  966.                     ELSIF res=5 THEN Texts.WriteString(W0, " module not found")
  967.                     ELSIF res=6 THEN
  968.                         IF Modules.importing[0]#CHR(0) THEN Texts.WriteString(W0, " command not found")
  969.                         ELSE Texts.OpenWriter (W0);
  970.                         END
  971.                     ELSE Texts.WriteString(W0, " res = "); Texts.WriteInt(W0, res, 0)
  972.                     END
  973.                 ELSIF res < 0 THEN
  974.                     IF i + 1 = S.len THEN Texts.OpenWriter(W0); res := 0  (*execution of module body*)
  975.                     ELSE
  976.                         INC(i); WHILE i < S.len DO Texts.Write(W0, S.s[i]); INC(i) END;
  977.                         Texts.WriteString(W0, " not found")
  978.                     END
  979.                 END;
  980.                 IF res # 0 THEN Texts.WriteLn(W0); Texts.Append(Oberon.Log, W0.buf) END
  981.             END
  982.         END
  983.     END Call;
  984.     PROCEDURE PickAttributes (VAR W: Texts.Writer; ch: CHAR; T: Texts.Text; pos: LONGINT; fnt: Fonts.Font; col, voff: SHORTINT);
  985.         VAR R: Texts.Reader; ch0: CHAR;
  986.     BEGIN
  987.         IF T.len > 0 THEN
  988.             IF pos < T.len THEN Texts.OpenReader(R, T, pos); Texts.Read(R, ch0) END;
  989.             IF (pos > 0) & ((pos = T.len) OR (ch0 <= " ")) THEN
  990.                 Texts.OpenReader(R, T, pos - 1); Texts.Read(R, ch0)
  991.             END;
  992.             Texts.SetFont(W, R.fnt); Texts.SetColor(W, R.col);
  993.             IF (ch = CR) OR (ch = TAB)  THEN Texts.SetOffset(W, voff) ELSE Texts.SetOffset(W, R.voff) END
  994.         ELSE Texts.SetFont(W, fnt); Texts.SetColor(W, col); Texts.SetOffset(W, voff)
  995.         END
  996.     END PickAttributes;
  997.     PROCEDURE ShiftBlock (F: Frame; delta: INTEGER);    (* shift selected lines to left or right *)
  998.         VAR text: Texts.Text; pos, beg, end, time: LONGINT; select: SelectMsg; ch: CHAR;
  999.     BEGIN
  1000.         Oberon.GetSelection(text, beg, end, time);
  1001.         IF (time >= 0) & (text = F.text) THEN BegOfLine(F.text, beg, FALSE); pos := beg;
  1002.             WHILE pos < end DO Texts.OpenReader(R, F.text, pos); Texts.Read(R, ch);
  1003.                 WHILE (R.elem # NIL) & (R.elem IS Parc) & (pos < end) DO Texts.Read(R, ch); INC(pos) END;
  1004.                 IF pos < end THEN
  1005.                     IF delta < 0 THEN
  1006.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN
  1007.                             Texts.Delete(F.text, pos, pos + 1); DEC(end)
  1008.                         END
  1009.                     ELSE
  1010.                         PickAttributes(W, TAB, text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  1011.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN Texts.Write(W, ch)    (* first char extension *)
  1012.                         ELSE Texts.Write(W, TAB)
  1013.                         END;
  1014.                         Texts.Insert(F.text, pos, W.buf); INC(end); INC(pos)
  1015.                     END;
  1016.                     Texts.OpenReader(R, F.text, pos);
  1017.                     REPEAT Texts.Read(R, ch) UNTIL R.eot OR (ch = CR);
  1018.                     pos := Texts.Pos(R)
  1019.                 END
  1020.             END;
  1021.             select.text := F.text; select.beg := beg; select.end := pos; select.time := Oberon.Time();
  1022.             Viewers.Broadcast(select)
  1023.         END
  1024.     END ShiftBlock;
  1025.     PROCEDURE Write (F: Frame; ch: CHAR; fnt: Fonts.Font; col, voff: SHORTINT);
  1026.         VAR loc: Location; parc: Parc; org, pos, pbeg: LONGINT; i: INTEGER;
  1027.             buf: ARRAY 32 OF CHAR; v: Viewers.Viewer; f: Frame;
  1028.             copy: Texts.CopyMsg; input: Oberon.InputMsg;
  1029.         PROCEDURE Visible(ch: CHAR): BOOLEAN;
  1030.             VAR pat: Display.Pattern; dx, x, y, w, h: INTEGER;
  1031.         BEGIN Display.GetChar(W.fnt.raster, ch, dx, x, y, w, h, pat); RETURN dx > 0
  1032.         END Visible;
  1033.         PROCEDURE InsertBuffer;
  1034.             VAR i, j: INTEGER; ch: CHAR;
  1035.         BEGIN i := 0; j := 0; ch := buf[i];
  1036.             WHILE ch # 0X DO
  1037.                 IF (ch = TAB) OR (ch = CR) OR (ch = " ") OR Visible(ch) THEN Texts.Write(W, ch); INC(j) END;
  1038.                 INC(i); ch := buf[i]
  1039.             END; 
  1040.             IF j > 0 THEN Texts.Insert(F.text, pos, W.buf); INC(pos, LONG(j)) END
  1041.         END InsertBuffer;
  1042.         PROCEDURE Flush;
  1043.             VAR ch: CHAR;
  1044.         BEGIN
  1045.             WHILE Input.Available() > 0 DO Input.Read(ch) END
  1046.         END Flush;
  1047.     BEGIN
  1048.         IF F.hasSel THEN
  1049.             IF ch = CRSL THEN ShiftBlock(F, -1)
  1050.             ELSIF ch = CRSR THEN ShiftBlock(F, 1)
  1051.             ELSIF ch < 80X THEN
  1052.                 pos := F.selbeg.pos; v := Viewers.Next(Viewers.This(F.X, F.Y));
  1053.                 IF (v.dsc # NIL) & (v.dsc.next # NIL) & (v.dsc.next IS Frame) THEN
  1054.                     f := v.dsc.next(Frame);
  1055.                     IF (f.text = F.text) & f.hasSel & (f.selbeg.pos < pos) THEN pos := f.selbeg.pos END
  1056.                 END;
  1057.                 Texts.Delete(F.text, pos, F.selend.pos); SetCaret(F, pos);
  1058.                 IF ch # DEL THEN Write(F, ch, fnt, col, voff) END
  1059.             END
  1060.         ELSIF F.hasCar THEN pos := F.carloc.pos;
  1061.             IF ch = DEL THEN
  1062.                 IF pos > F.org THEN DEC(pos); Texts.Delete(F.text, pos, pos + 1); Flush END
  1063.             ELSIF (ch = DELRIGHT) & (pos < F.text.len) THEN Texts.Delete(F.text, pos, pos + 1); Flush        (*<< mah del right *)
  1064.             ELSIF ch = HOME THEN pos := Pos (F, F.X, F.carloc.y)                (*<< mah beg of line *)
  1065.             ELSIF ch = EOL THEN pos := Pos (F, F.X+F.W-1, F.carloc.y)        (*<< mah end of line *)
  1066.             ELSIF (ch = CRSL) & (pos > 0) THEN DEC(pos)
  1067.             ELSIF (ch = CRSR) & (pos < F.text.len) THEN INC(pos)
  1068.             ELSIF (ch = CRSU) & (pos > 0) THEN                                    (*<< mah cursor up *)
  1069.                 org:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h);
  1070.                 IF org=pos THEN Show (F, F.org-1) END;
  1071.                 pos:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h)
  1072.             ELSIF (ch = CRSD) & (pos < F.text.len) THEN                        (*<< mah cursor down *)
  1073.                 org:=Pos (F, F.carloc.x+1, F.carloc.y-F.carloc.line.next.h);
  1074.                 IF (org=pos) & (F.trailer.org+F.trailer.len#F.text.len) THEN Show (F, F.trailer.next.next.org) END;
  1075.                 LocatePos (F, pos, loc);
  1076.                 pos:=Pos (F, F.carloc.x+1, loc.y-loc.line.next.h)
  1077.             ELSIF ch=PGUP THEN                                                    (*<< mah page up *)
  1078.                 LocateLine (F, F.Y+F.H-1, loc); i:=loc.y-F.Y-F.bot;
  1079.                 Back (F, i, pos); Show (F, pos); pos:=F.org
  1080.             ELSIF ch=PGDN THEN                                                (*<< mah page down *)
  1081.                 IF F.trailer.org+F.trailer.len = F.text.len THEN pos:=F.trailer.org
  1082.                 ELSE LocateLine (F, F.Y, loc); Show (F, loc.org); pos:=F.org
  1083.                 END
  1084.             ELSIF (ch = CRSL) OR (ch = CRSU) OR (ch = CRSD) OR (ch = CRSR) OR (ch = PGUP) OR (ch=PGDN) THEN 
  1085.             ELSIF (ch = BRK) OR (ch = ShiftBRK) THEN
  1086.                 ParcBefore(F.text, pos, P, pbeg); P.handle(P, copy); parc := copy.e(Parc);
  1087.                 IF ch = BRK THEN EXCL(parc.opts, pageBreak) ELSE INCL(parc.opts, pageBreak) END;
  1088.                 PickAttributes(W, Texts.ElemChar, F.text, pos, fnt, col, voff);
  1089.                 Texts.WriteElem(W, parc); Texts.Insert(F.text, pos, W.buf); INC(pos)
  1090.             ELSIF (ch = TAB) OR (ch = CR) OR (ch >= " ") THEN
  1091.                 PickAttributes(W, ch, F.text, pos, fnt, col, voff);
  1092.                 IF ch = CR THEN buf[0] := CR; i := 1; org := F.carloc.org; BegOfLine(F.text, org, FALSE);
  1093.                     Texts.OpenReader(R, F.text, org);
  1094.                     REPEAT Texts.Read(R, ch) UNTIL (R.elem = NIL) OR ~(R.elem IS Parc);
  1095.                     WHILE (Texts.Pos(R) <= pos) & (ch <= " ") & (ch # Texts.ElemChar) & (i < 31) DO
  1096.                         buf[i] := ch; INC(i); Texts.Read(R, ch)
  1097.                     END
  1098.                 ELSIF ch = LF THEN buf[0] := CR; i:=1            (*<< mah Enter on numeric pad has no autoindent *)
  1099.                 ELSE buf[0] := ch; i := 1
  1100.                 END;
  1101.                 buf[i] := 0X; InsertBuffer
  1102.             END;
  1103.             IF pos < F.org THEN Show(F, F.org - 1) END;
  1104.             SetCaret(F, pos);
  1105.             WHILE F.carloc.y < F.Y + F.bot DO Show(F, F.trailer.next.next.org); Flush; SetCaret(F, pos) END
  1106.         ELSIF F.focus # NIL THEN input.id := Oberon.consume; input.ch := ch;
  1107.             input.fnt := fnt; input.col := col; input.voff := voff; F.focus.handle(F.focus, input)
  1108.         END
  1109.     END Write;
  1110.     PROCEDURE TouchElem (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  1111.         VAR loc: Location; e: Texts.Elem; pbeg: LONGINT; y0: INTEGER;
  1112.             track: TrackMsg;
  1113.     BEGIN
  1114.         LocateChar(F, x, y, loc); e := R.elem;
  1115.         IF (e # NIL) & (loc.x + e.W DIV Unit <= F.X + F.W - F.right) THEN
  1116.             ParcBefore(F.text, loc.pos, P, pbeg); y0 := loc.y + loc.line.dsr - SHORT(P.dsr DIV Unit) + loc.dy;
  1117.             IF (loc.x <= x) & (x < loc.x + e.W DIV Unit) & (keysum= {middleKey}) THEN
  1118.                 track.X := x; track.Y := y; track.keys := keysum;
  1119.                 track.fnt := R.fnt; track.col := R.col; track.pos := Texts.Pos(R) - 1;
  1120.                 track.frame := F; track.X0 := loc.x; track.Y0 := y0;
  1121.                 e.handle(e, track); keysum := {}
  1122.             END
  1123.         END
  1124.     END TouchElem;
  1125.     PROCEDURE Edit (F: Frame; x, y: INTEGER; keysum: SET);
  1126.         VAR ef: Display.Frame; pos, pos0, i: LONGINT; keys: SET; input: Oberon.InputMsg; loc: Location;
  1127.     BEGIN
  1128.         IF x < F.X + F.barW THEN
  1129.             IF keysum # {} THEN scrollBar.HandleScrollBar(F, x, y, keysum)
  1130.             ELSE DrawCursor(x, y)
  1131.             END
  1132.         ELSE    (* text area *)
  1133.             ef := ThisSubFrame(F, x, y);
  1134.             IF ef # NIL THEN    (* within sub-frame *)
  1135.                 IF (F.focus # ef) & (keysum = {leftKey}) THEN
  1136.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1137.                     IF keysum = {leftKey} THEN RemoveSelection(F); RemoveCaret(F); PassSubFocus(F, ef); RETURN END
  1138.                 ELSIF F.focus = ef THEN input.id := Oberon.track; input.keys := keysum; input.X := x; input.Y := y;
  1139.                     ef.handle(ef, input); RETURN
  1140.                 END
  1141.             END;
  1142.             IF keysum # {} THEN TouchElem(F, x, y, keysum);
  1143.                 IF keysum = {} THEN RETURN END
  1144.             END;
  1145.             IF leftKey IN keysum THEN
  1146.                 Oberon.PassFocus(Viewers.This(F.X, F.Y)); pos0 := Pos(F, x, y);
  1147.                 IF (F.carloc.pos = pos0) & (pos0 # F.text.len) THEN
  1148.                     IF F.hasSel & (F.selbeg.pos <= pos0) & (pos0 < F.selend.pos) THEN
  1149.                         LocateLine(F, y, loc); SetSelection(F, loc.org, loc.org + loc.line.span);
  1150.                         INCL(keysum, trackLine); TrackSelection(F, x, y, keysum)
  1151.                     ELSE
  1152.                         RemoveCaret(F);
  1153.                         SetSelection(F, WordBeg(F.text, pos0), WordEnd(F.text, pos0));
  1154.                         INCL(keysum, trackWord); TrackSelection(F, x, y, keysum)
  1155.                     END
  1156.                 ELSE
  1157.                     RemoveSelection(F);
  1158.                     REPEAT
  1159.                         TrackMouse(x, y, keys, keysum); pos := Pos(F, x, y); SetCaret(F, pos)
  1160.                     UNTIL (keys = {}) OR (pos # pos0);
  1161.                     IF pos # pos0 THEN
  1162.                         RemoveCaret(F);
  1163.                         IF pos < pos0 THEN i := pos; pos := pos0; pos0 := i END; 
  1164.                         SetSelection(F, pos0, pos); TrackSelection(F, x, y, keysum)
  1165.                     END
  1166.                 END
  1167.             ELSIF middleKey IN keysum THEN TrackWord(F, x, y, pos, keysum);
  1168.                 IF keysum # cancel THEN Call(F, pos, keysum = {middleKey, leftKey}) END
  1169.             ELSE DrawCursor(x, y)
  1170.             END
  1171.         END
  1172.     END Edit;
  1173.     (** General **)
  1174.     PROCEDURE Copy (SF, DF: Frame);
  1175.     BEGIN
  1176.         DF.handle := SF.handle; DF.text := SF.text; DF.org := SF.org;
  1177.         DF.col := SF.col; DF.left := SF.left; DF.right := SF.right; DF.top := SF.top; DF.bot := SF.bot;
  1178.         DF.barW := SF.barW; DF.hasCar := FALSE; DF.hasSel := FALSE; DF.showsParcs := SF.showsParcs;
  1179.         DF.focus := NIL; DF.trailer := NIL
  1180.     END Copy;
  1181.     PROCEDURE Handle* (f: Display.Frame; VAR msg: Display.FrameMsg);
  1182.         VAR F, F1: Frame; pos: LONGINT;
  1183.     BEGIN F := f(Frame);
  1184.         IF msg IS Oberon.InputMsg THEN
  1185.             WITH msg: Oberon.InputMsg DO
  1186.                 IF msg.id = Oberon.consume THEN Write(F, msg.ch, msg.fnt, msg.col, msg.voff)
  1187.                 ELSIF msg.id = Oberon.track THEN 
  1188.                     IF (msg.X < F.X + F.barW) THEN        (*CM  18.11.94 *)
  1189.                         scrollBar.HandleScrollBar(F, msg.X, msg.Y, msg.keys)
  1190.                     ELSIF (msg.X >= F.X + F.barW) THEN
  1191.                         Edit(F, msg.X, msg.Y, msg.keys)
  1192.                     END
  1193.                 END
  1194.             END
  1195.         ELSIF msg IS Oberon.ControlMsg THEN
  1196.             WITH msg: Oberon.ControlMsg DO
  1197.                 IF msg.id = Oberon.defocus THEN RemoveCaret(F)
  1198.                 ELSIF msg.id = Oberon.neutralize THEN
  1199.                     RemoveCaret(F); RemoveSelection(F); PassSubFocus(F, NIL); NotifySubFrames(F, msg)
  1200.                 ELSE NotifySubFrames(F, msg)
  1201.                 END
  1202.             END
  1203.         ELSIF msg IS Oberon.CopyMsg THEN
  1204.             WITH msg: Oberon.CopyMsg DO
  1205.                 IF msg.F = NIL THEN NEW(F1); msg.F := F1 END;
  1206.                 Copy(F, msg.F(Frame))
  1207.             END
  1208.         ELSIF msg IS UpdateMsg THEN NotifySubFrames(F, msg);
  1209.             WITH msg: UpdateMsg DO
  1210.                 IF msg.text = F.text THEN Update(F, msg) END
  1211.             END
  1212.         ELSIF msg IS InsertElemMsg THEN
  1213.             IF F.hasCar THEN pos := F.carloc.pos;
  1214.                 PickAttributes(W, Texts.ElemChar, F.text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  1215.                 Texts.WriteElem(W, msg(InsertElemMsg).e);
  1216.                 Texts.Insert(F.text, pos, W.buf);
  1217.                 SetCaret(F, pos + 1)
  1218.             END
  1219.         ELSIF msg IS Oberon.SelectionMsg THEN NotifySubFrames(F, msg);
  1220.             WITH msg: Oberon.SelectionMsg DO
  1221.                 IF F.hasSel & (F.time > msg.time) THEN
  1222.                     msg.text := F.text; msg.beg := F.selbeg.pos; msg.end := F.selend.pos; msg.time := F.time
  1223.                 END
  1224.             END
  1225.         ELSIF msg IS Oberon.CopyOverMsg THEN NotifySubFrames(F, msg);
  1226.             WITH msg: Oberon.CopyOverMsg DO
  1227.                 IF F.hasSel THEN Write(F, DEL, NIL, 0, 0) END;
  1228.                 IF F.hasCar THEN Texts.Save(msg.text, msg.beg, msg.end, B);
  1229.                     Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (msg.end - msg.beg))
  1230.                 END
  1231.             END
  1232.         ELSIF msg IS MenuViewers.ModifyMsg THEN
  1233.             WITH msg: MenuViewers.ModifyMsg DO
  1234.                 F.handle(F, neutralize); Resize(F, F.X, msg.Y, F.W, msg.H)
  1235.             END
  1236.         ELSIF msg IS SelectMsg THEN NotifySubFrames(F, msg);
  1237.             WITH msg: SelectMsg DO
  1238.                 IF (msg.text = F.text) & ~F.hasSel THEN Oberon.RemoveMarks(F.X, F.Y, F.W, F.H);
  1239.                     F.handle(F, neutralize);
  1240.                     SetSelection(F, msg.beg, msg.end); F.time := msg.time;
  1241.                     IF F.hasSel THEN F.selbeg.pos := msg.beg; F.selend.pos := msg.end END
  1242.                 END
  1243.             END
  1244.         ELSE NotifySubFrames(F, msg)
  1245.         END
  1246.     END Handle;
  1247.     PROCEDURE Open* (F: Frame; T: Texts.Text; pos: LONGINT);
  1248.     BEGIN
  1249.         F.handle := Handle; F.text := T; F.org := pos; F.col := Display.black;
  1250.         F.left := left; F.right := right; F.top := top; F.bot := bot;
  1251.         F.barW := barW; F.hasCar := FALSE; F.hasSel := FALSE; F.showsParcs := FALSE; F.trailer := NIL
  1252.     END Open;
  1253.     PROCEDURE NotifyDisplay* (T: Texts.Text; op: INTEGER; beg, end: LONGINT);
  1254.         VAR msg: UpdateMsg;
  1255.     BEGIN
  1256.         msg.text := T; msg.id := op; msg.beg := beg; msg.end := end; Viewers.Broadcast(msg)
  1257.     END NotifyDisplay;
  1258.     PROCEDURE Text* (name: ARRAY OF CHAR): Texts.Text;
  1259.         VAR text: Texts.Text;
  1260.     BEGIN
  1261.         NEW(text); Texts.Open(text, name); text.notify := NotifyDisplay; RETURN text
  1262.     END Text;
  1263.     PROCEDURE NewText* (T: Texts.Text; pos: LONGINT): Frame;
  1264.         VAR frame: Frame;
  1265.     BEGIN
  1266.         NEW(frame); Open(frame, T, pos);
  1267.         RETURN frame
  1268.     END NewText;
  1269.     PROCEDURE NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  1270.         VAR T, T1: Texts.Text; buf: Texts.Buffer; frame: Frame; fn: ARRAY 32 OF CHAR; i: INTEGER;
  1271.     BEGIN
  1272.         T := Text("");
  1273.         Texts.WriteString(W0, name); Texts.WriteString(W0, " | "); Texts.Append(T, W0.buf);
  1274.         IF commands[0] = "^" THEN
  1275.             i := 0; REPEAT INC(i); fn[i-1] := commands[i] UNTIL commands[i] = 0X;
  1276.             IF Files.Old(fn) = NIL THEN
  1277.                 Texts.WriteString(W0, "System.Close  System.Grow  System.Copy Edit.Store "); Texts.Append(T, W0.buf)
  1278.             ELSE
  1279.                 NEW(T1); Texts.Open(T1, fn);
  1280.                 NEW(buf); Texts.OpenBuf(buf); Texts.Save(T1, 0, T1.len, buf); Texts.Append(T, buf)
  1281.             END
  1282.         ELSE
  1283.             Texts.WriteString(W0, commands); Texts.Append(T, W0.buf)
  1284.         END;
  1285.         NEW(frame); Open(frame, T, 0);
  1286.         frame.col := Display.white; frame.left := 6; frame.top := 0; frame.bot := 0; frame.barW := 0;
  1287.         RETURN frame
  1288.     END NewMenu;
  1289. (* CM 19.07.94 *)
  1290.     PROCEDURE (obj: ScrollBarElem) LineDown;
  1291.     VAR F: Display.Frame; loc: Location;
  1292.     BEGIN
  1293.         F := obj.F;
  1294.         WITH F: Frame DO
  1295.             Show(F, F.org  - 1);
  1296.             LocateLine(F, F.Y+F.bot, loc);
  1297.             obj.Update(FALSE, F.text.len, F.org, loc.org + loc.line.len)
  1298.         ELSE
  1299.         END
  1300.     END LineDown;
  1301.     PROCEDURE (obj: ScrollBarElem) LineUp;
  1302.     VAR F: Display.Frame; loc: Location;
  1303.     BEGIN
  1304.         F := obj.F;
  1305.         WITH F: Frame DO
  1306.             Show(F, F.trailer.next.next.org);
  1307.             LocateLine(F, F.Y+F.bot, loc);
  1308.             obj.Update(FALSE, F.text.len, F.org, loc.org + loc.line.len)
  1309.         ELSE
  1310.         END
  1311.     END LineUp;
  1312.     PROCEDURE (obj: ScrollBarElem) PageDown;
  1313.     VAR F: Display.Frame; loc: Location;
  1314.     BEGIN
  1315.         F := obj.F;
  1316.         WITH F: Frame DO
  1317.             LocateLine(F, F.Y + F.bot, loc);
  1318.             Show(F, loc.org);
  1319.             LocateLine(F, F.Y + F.bot, loc);
  1320.             obj.Update(FALSE, F.text.len, F.org, loc.org + loc.line.len)
  1321.         ELSE
  1322.         END
  1323.     END PageDown;
  1324.     PROCEDURE (obj: ScrollBarElem) PageUp;
  1325.     VAR F: Display.Frame;loc: Location; delta: INTEGER; org: LONGINT;
  1326.     BEGIN
  1327.         F := obj.F;
  1328.         WITH F: Frame DO
  1329.             LocateLine(F, F.Y + F.H - 1, loc);
  1330.             org := loc.org;
  1331.             delta := loc.y - F.Y - F.bot;
  1332.             Back(F, delta, org);
  1333.             Show(F, org);
  1334.             LocateLine(F, F.Y + F.bot, loc);
  1335.             obj.Update(FALSE, F.text.len, F.org, loc.org + loc.line.len)
  1336.         ELSE
  1337.         END
  1338.     END PageUp;
  1339.     PROCEDURE (obj: ScrollBarElem) UpdateView(pressed: BOOLEAN; dY: INTEGER);
  1340.     VAR F: Display.Frame; org: LONGINT; t: TextLine;
  1341.     BEGIN
  1342.         F := obj.F;
  1343.         IF dY # obj.sliderdY THEN
  1344.             WITH F: Frame DO
  1345.                 org := (F.text.len * dY) DIV (obj.h - obj.w*2 - obj.minW - obj.minW DIV 2);
  1346.                 IF dY > obj.sliderdY THEN
  1347.                     IF (org <= F.org) OR (org < F.trailer.next.next.org) THEN org := F.trailer.next.next.org
  1348.                     END
  1349.                 ELSIF dY < obj.sliderdY THEN
  1350.                     IF org >= F.org THEN org := F.org - 1  END
  1351.                 END;
  1352.                 IF org # F.org THEN
  1353.                     Show(F, org);
  1354.                     obj.Update(pressed, F.text.len, F.org, F.trailer.org+F.trailer.len)
  1355.                 END
  1356.             ELSE
  1357.             END
  1358.         END
  1359.     END UpdateView;
  1360.     PROCEDURE (obj: ScrollBarElem) TrackSlider(VAR mx, my : INTEGER; VAR keysum : SET);
  1361.     VAR pos: LONGINT; F: Display.Frame;
  1362.     BEGIN
  1363.         obj.TrackSlider^(mx, my, keysum);
  1364.         F := obj.F;
  1365.         WITH F: Frame DO
  1366.             IF keysum = {middleKey, leftKey} THEN
  1367.                 pos := F.text.len;
  1368.                 Back(F, F.H - F.top - F.bot - 30 (* heuristic*), pos); Show(F,pos); 
  1369.                 obj.CalculateSliderDim(F.text.len, F.org, F.trailer.org);
  1370.                 obj.DrawSlider(FALSE)
  1371.             ELSIF keysum = {middleKey, rightKey} THEN
  1372.                 obj.UpdateView(FALSE, 0)
  1373.             END
  1374.         ELSE
  1375.         END
  1376.     END TrackSlider;
  1377.     PROCEDURE (obj: ScrollBarElem) HandleScrollBar (F: Display.Frame; mx, my : INTEGER; 
  1378.                                                                                         keysum : SET);
  1379.     BEGIN
  1380.         WITH F: Frame DO
  1381.             obj.SetDim(F.X, F.Y, F.barW -1, F.H);
  1382.             obj.CalculateSliderDim(F.text.len, F.org, F.trailer.org + F.trailer.len);
  1383.             used := TRUE;
  1384.             obj.HandleScrollBar^(F, mx, my, keysum);
  1385.             used := FALSE
  1386.         ELSE
  1387.         END
  1388.     END HandleScrollBar;
  1389. BEGIN
  1390.     Texts.OpenWriter(W); Texts.OpenWriter(W0);
  1391.     Texts.SetFont(W0, Fonts.Default); Texts.SetColor(W0, Display.white); Texts.SetOffset(W0, 0);
  1392.     neutralize.id := Oberon.neutralize;
  1393.     NEW(par);
  1394.     NEW(B); Texts.OpenBuf(B);
  1395.     menuH := Fonts.Default.height + 2;
  1396.     barW := menuH; left := barW + 6; right := 8; top := 6; bot := 6;            (*CM 08.08.94*)
  1397.     InitDefParc;
  1398.     NEW(scrollBar); scrollBar.Init;     used := FALSE;                                    (*CM 08.08.94*)
  1399. END TextFrames.
  1400.